package com.right.health.app.net;

import com.magics.http.model.HttpParams;
import com.right.health.app.bean.DrugReminderInfo;
import com.right.health.app.bean.DrugReminderResponse;
import com.right.health.app.bean.ReviewReminderInfo;
import com.right.health.app.bean.ReviewReminderResponse;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/*
 * Created by hopeliao on 2019-11-23
 *
 */
public class RemindApi extends IBaseApi {

     public void getDrugRemindList(int pageNo, String memberid) {
         HttpParams params = new HttpParams();
         try {
             JSONObject jsonObject = new JSONObject();
             jsonObject.put("memberid", memberid);
             jsonObject.put("remindType", "0");

             PageUtil.addPageJson(jsonObject, pageNo);

             params.addFormParam("jsonstr", jsonObject.toString());
         } catch (JSONException e) {
             e.printStackTrace();
         }
         doPostJson(HttpConstants.API_GET_LAST_REMIND_LIST, params, DrugReminderResponse.class);
     }

     public void addDrugRemind(List<DrugReminderInfo> infos, String memberid) {
         HttpParams params = new HttpParams();

         try {
             JSONArray array = new JSONArray();
            for(DrugReminderInfo info:infos) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("memberid", memberid);
                jsonObject.put("remindName", info.remindname);
                jsonObject.put("startTime", info.starttime);
                jsonObject.put("endTime", info.endtime);
                jsonObject.put("remindType", "0");

                String[] netCycleTimes  = info.netCycleTimes.split(",");
                JSONArray cycleArray = new JSONArray();
                for(String str:netCycleTimes) {
                    cycleArray.put(str);
                }
                jsonObject.put("cycletimes", cycleArray);

                array.put(jsonObject);
            }
            JSONObject root = new JSONObject();
            root.put("reminds", array);
             params.addFormParam("jsonstr", root.toString());
         } catch (JSONException e) {
             e.printStackTrace();
         }

         doPostJson(HttpConstants.API_ADD_REMIND, params, null);
     }

     public void modifyTakeMedicine(String cycletimesid, int state) {
         HttpParams params = new HttpParams();

         try {
             JSONObject jsonObject = new JSONObject();
             jsonObject.put("cycletimesid", cycletimesid);
             jsonObject.put("iseat", state);

             params.addFormParam("jsonstr", jsonObject.toString());
         } catch (JSONException e) {
             e.printStackTrace();
         }

         doPostJson(HttpConstants.API_UPDATE_EAT, params, null);
     }

     public void modifyRemind(String remindid, String remindstatus) {
         HttpParams params = new HttpParams();

         try {
             JSONObject jsonObject = new JSONObject();
             jsonObject.put("remindid", remindid);
             jsonObject.put("remindstatus", remindstatus);

             params.addFormParam("jsonstr", jsonObject.toString());
         } catch (JSONException e) {
             e.printStackTrace();
         }

         doPostJson(HttpConstants.API_UPDATE_REMIND, params, null);
     }

     public void delete(String remindid) {
         HttpParams params = new HttpParams();
         try {
             JSONObject jsonObject = new JSONObject();
             jsonObject.put("remindid", remindid);

             params.addFormParam("jsonstr", jsonObject.toString());
         } catch (JSONException e) {
             e.printStackTrace();
         }
         doPostJson(HttpConstants.API_DELETE_REMIND, params, null);
     }

     //添加复诊
     public void addReviewReminder(String memberid, List<ReviewReminderInfo> dataSource) {
         HttpParams params = new HttpParams();
         try {
             JSONArray array = new JSONArray();
             for(ReviewReminderInfo info:dataSource) {
                 JSONObject item = new JSONObject();
                 item.put("memberid", memberid);
                 item.put("visitagainname", info.visitagainname);
                 item.put("nextvisittime", info.nextvisittime);
//                 item.put("nextremindtime", info.visitendtime);
                 item.put("visitendtime", info.visitendtime);
                 item.put("visittype", info.visittype);
                 item.put("visitdesc", info.visitdesc);
                 item.put("remindtype", info.remindtype);
                 array.put(item);
             }
             JSONObject root = new JSONObject();
             root.put("visits", array);
             params.addFormParam("jsonstr", root.toString());
         } catch (JSONException e) {
             e.printStackTrace();
         }
         doPostJson(HttpConstants.API_ADD_REVIEW_REMINDER, params, null);
     }

     public void getReviewReminder(String memberid, int pageNo) {
         HttpParams params = new HttpParams();
         try {
             JSONObject jsonObject = new JSONObject();
             jsonObject.put("memberid", memberid);
             PageUtil.addPageJson(jsonObject, pageNo);
             params.addFormParam("jsonstr", jsonObject.toString());
         } catch (JSONException e) {
             e.printStackTrace();
         }
         doPostJson(HttpConstants.API_GET_REVIEW_REMINDER, params, ReviewReminderResponse.class);
     }

     public void modifyReviewRemind(String visitagainid, String remindstatus) {
         HttpParams params = new HttpParams();
         try {
             JSONObject jsonObject = new JSONObject();
             jsonObject.put("visitagainid", visitagainid);
             jsonObject.put("remindstatus", remindstatus);

             params.addFormParam("jsonstr", jsonObject.toString());
         } catch (JSONException e) {
             e.printStackTrace();
         }
         doPostJson(HttpConstants.API_UPDATE_REVIEW_REMINDER, params, null);
     }

    public void deleteReviewRemind(String id) {
        HttpParams params = new HttpParams();
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("visitagainid", id);

            params.addFormParam("jsonstr", jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        doPostJson(HttpConstants.API_DELETE_REVIEW_REMINDER, params, null);
    }
}
